package com.jz.digital.net;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author WangYong
 * @date 2024/4/9 16:32
 * @description:自定义接口读取超时时间
 */
public class TimeoutInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request originalRequest = chain.request();
        // 通过自定义的超时时间头信息获取超时时间
        long timeout = getTimeoutForRequest(originalRequest);
        // 构造新的请求，添加超时时间头信息
        Request newRequest = originalRequest.newBuilder()
                .header("Timeout", String.valueOf(timeout))
                .header("ReadTimeout", String.valueOf(timeout))
                .header("WriteTimeout", String.valueOf(timeout))
                .build();

        return chain.proceed(newRequest);
    }

    // 根据请求自定义超时时间的逻辑
    private long getTimeoutForRequest(Request request) {
        // 返回超时时间的毫秒数
        return NetConfiguration.Builder.TimeOut;
    }
}
